//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    mobile: '',
    codeText: '获取验证码'
  },
  onLoad: function () {
    this.setData({
      userInfo: app.globalData.userInfo,
    })
  },
  formSubmit: function (e) {
    if (e.detail.value.name == '') {
      wx.$showToast('姓名不可为空哦！')
      return;
    }
    if (e.detail.value.mobilePhone == '') {
      wx.$showToast('手机号码不可为空哦！')
      return;
    }
    if (e.detail.value.code == '') {
      wx.$showToast('验证码不可为空哦！')
      return;
    }
    // 请求方法
    console.log(app.globalData.openId)
    console.log(e.detail.value)
    wx.request({
      url: app.globalData.prefix + "/memberAction/addInfo.do",
      data: {
        openId: app.globalData.userInfo.openId,
        code: e.detail.value.code,
        mobile: e.detail.value.mobilePhone,
        realName: e.detail.value.name
      },
      method: 'POST',
      success: (res) => {
        let status = res.data.status
        if (status == "00") {
          wx.switchTab({
            url: '../index/index'
          })
        } else {
          wx.$showToast(res.data.errorMsg)
        }
      }
    })
  },
  formReset: function () {
    console.log('form发生了reset事件')
  },
  mobileInput: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  getCode() {
    if (this.data.codeText === '获取验证码') {
      wx.request({
        url: app.globalData.prefix + "/smsAction/sendMessage.do",
        data: {
          mobile: this.data.mobile
        },
        success: (res) => {
          let status = res.data.status
          if (status == "00") {
            let time = 60
            let djs = setInterval(() => {
              time--
              if (time <= 0) {
                this.setData({
                  codeText: '获取验证码'
                })
                clearInterval(djs)
              } else {
                this.setData({
                  codeText: time + 's'
                })
              }
            }, 1000)
            wx.$showToast(res.data.result)
          } else {
            wx.$showToast(res.data.errorMsg)
          }
        }
      })
    } else {
      wx.$showToast('正在获取验证码，请稍等')
    }
  },
})